// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.worldMap;

import java.util.ArrayList;
import se.krka.kahlua.vm.KahluaTable;
import se.krka.kahlua.vm.KahluaTableIterator;
import zombie.Lua.LuaManager;
import zombie.util.Type;
import zombie.util.list.PZArrayUtil;

public final class MapDefinitions {
    private static MapDefinitions instance;
    private final ArrayList<String> m_definitions = new ArrayList();

    public static MapDefinitions getInstance() {
        if (instance == null) {
            instance = new MapDefinitions();
        }

        return instance;
    }

    public String pickRandom() {
        if (this.m_definitions.isEmpty()) {
            this.initDefinitionsFromLua();
        }

        return this.m_definitions.isEmpty() ? "Default" : PZArrayUtil.pickRandom(this.m_definitions);
    }

    private void initDefinitionsFromLua() {
        KahluaTable kahluaTable0 = Type.tryCastTo(LuaManager.env.rawget("LootMaps"), KahluaTable.class);
        if (kahluaTable0 != null) {
            KahluaTable kahluaTable1 = Type.tryCastTo(kahluaTable0.rawget("Init"), KahluaTable.class);
            if (kahluaTable1 != null) {
                KahluaTableIterator kahluaTableIterator = kahluaTable1.iterator();

                while (kahluaTableIterator.advance()) {
                    String string = Type.tryCastTo(kahluaTableIterator.getKey(), String.class);
                    if (string != null) {
                        this.m_definitions.add(string);
                    }
                }
            }
        }
    }

    public static void Reset() {
        if (instance != null) {
            instance.m_definitions.clear();
            instance = null;
        }
    }
}
